using System;
using System.Data;
using System.Data.SqlClient;

namespace MessageBoard
{
	/// <summary>
	/// Summary description for Language.
	/// </summary>
	public class Language : Connection
	{
		private int _Id;
		private string _Title;
		private string _Code;

     	public Language()
		{

		}

		public static Language Get(System.Data.DataRow dr)
		{
			Language language = new Language();
			language.Id = int.Parse(dr["Id"].ToString());
			language.Title = dr["Title"].ToString();
			language.Code = dr["Code"].ToString();
			return language;
		}

		public static Language Get(int id)
		{
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter("SELECT * FROM " + Connection.tblLanguage + " WHERE [Id] = @Id", DbConnection);
			getData.SelectCommand.Parameters.Add("@Id", id);
			getData.Fill(ds);
			if(ds.Tables[0].Rows.Count>0)
			{
				return Get(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		public static Language Get(string code)
		{
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter("SELECT * FROM " + Connection.tblLanguage + " WHERE [Code] = @Code", DbConnection);
			getData.SelectCommand.Parameters.Add("@Code", code);
			getData.Fill(ds);
			if(ds.Tables[0].Rows.Count>0)
			{
				return Get(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		public static System.Collections.ArrayList GetAll()
		{
			System.Collections.ArrayList arrayList = new System.Collections.ArrayList();
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter("SELECT * FROM " + Connection.tblLanguage + "", DbConnection);
			getData.Fill(ds);
			foreach(System.Data.DataRow dr in ds.Tables[0].Rows)
			{
				arrayList.Add(Get(dr));
			}
			return arrayList;
		}

		
		public void Insert()
		{
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter(@"
			INSERT INTO " + Connection.tblLanguage + @" 
			([Title], [Code])
			VALUES
			(@Title, @Code);
			SELECT @@identity as i", DbConnection);
			getData.SelectCommand.Parameters.Add("@Title", Title);
			getData.SelectCommand.Parameters.Add("@Code", Code);
			getData.Fill(ds);
			if(ds.Tables[0].Rows.Count>0)
			{
				Id = int.Parse(ds.Tables[0].Rows[0]["i"].ToString());
			}
		}

		public void Update()
		{
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter(@"
			UPDATE " + Connection.tblLanguage + @" SET 
			[Title] = @Title,
			[Code] = @Code
			WHERE [Id] = @Id", DbConnection);
			getData.SelectCommand.Parameters.Add("@Title", Title);
			getData.SelectCommand.Parameters.Add("@Code", Code);
			getData.SelectCommand.Parameters.Add("@Id", Id);
			getData.Fill(ds);
		}

				
		#region Properties
		/// <summary>
		/// The Id of the thread.
		/// </summary>
		public int Id
		{
			get
			{
				return _Id;
			}
			set
			{
				_Id = value;
			}
		}

		/// <summary>
		/// The Title.
		/// </summary>
		public string Title
		{
			get
			{
				return _Title;
			}
			set
			{
				_Title = value;
			}
		}

		/// <summary>
		/// The Code.
		/// </summary>
		public string Code
		{
			get
			{
				return _Code;
			}
			set
			{
				_Code = value;
			}
		}

		
		#endregion
	}
}
